package com.sfx.shortLink.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sfx.shortLink.common.constants.RedisKeyConstant;
import com.sfx.shortLink.common.convention.exception.ClientException;
import com.sfx.shortLink.common.database.BaseDO;
import com.sfx.shortLink.dao.entity.ShortLinkDO;
import com.sfx.shortLink.dao.mapper.ILinkMapper;
import com.sfx.shortLink.dto.req.PageRecycleBinReqDTO;
import com.sfx.shortLink.dto.req.RecoverRecycleBinReqDTO;
import com.sfx.shortLink.dto.req.RemoveRecycleBinReqDTO;
import com.sfx.shortLink.dto.req.SaveRecycleBinReqDTO;
import com.sfx.shortLink.dto.resp.PageRecycleBinRespDTO;
import com.sfx.shortLink.service.IRecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl implements IRecycleBinService {

    private final ILinkMapper shortLinkMapper;
    private final StringRedisTemplate stringRedisTemplate;


    @Override
    public void saveRecycleBin(SaveRecycleBinReqDTO requestParam) {
        String fullShortUrl = requestParam.getFullShortUrl();
        LambdaQueryWrapper<ShortLinkDO> queryWrapper = Wrappers.lambdaQuery(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, fullShortUrl)
                .eq(ShortLinkDO::getEnableStatus, 0);
        ShortLinkDO shortLinkDO = shortLinkMapper.selectOne(queryWrapper);
        if (shortLinkDO == null) {
            throw new ClientException("没有该短链接");
        }
        ShortLinkDO linkDO = ShortLinkDO.builder()
                .enableStatus(1)
                .build();
        shortLinkMapper.update(linkDO, queryWrapper);
        // 删除一下缓存
        stringRedisTemplate.delete(RedisKeyConstant.GOTO_SHORT_LINK_KEY + fullShortUrl);
    }


    @Override
    public IPage<PageRecycleBinRespDTO> pageRecycleBin(PageRecycleBinReqDTO requestParam) {
        LambdaQueryWrapper<ShortLinkDO> queryWrapper = Wrappers.lambdaQuery(ShortLinkDO.class)
                .eq(ShortLinkDO::getEnableStatus, 1);

        IPage<ShortLinkDO> resultPage = shortLinkMapper.selectPage(requestParam, queryWrapper);
        return resultPage.convert(each -> {
            PageRecycleBinRespDTO result = BeanUtil.toBean(each, PageRecycleBinRespDTO.class);
            result.setDomain("http://" + result.getDomain());
            return result;
        });
    }


    @Override
    public void recoverRecycleBin(RecoverRecycleBinReqDTO requestParam) {
        // 将这个短链接启用标识设置为0
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 1);
        ShortLinkDO linkDO = ShortLinkDO.builder()
                .enableStatus(0).build();
        shortLinkMapper.update(linkDO, updateWrapper);
        // 删除掉空值缓存
        stringRedisTemplate.delete(RedisKeyConstant.GOTO_IS_NULL_SHORT_LINK_KEY + requestParam.getFullShortUrl());
    }

    @Override
    public void removeRecycleBin(RemoveRecycleBinReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .set(ShortLinkDO::getDelTime, System.currentTimeMillis())
                .set(BaseDO::getDelFlag, 1);
        shortLinkMapper.update(null, updateWrapper);
    }
}